package com.tech.dcyao.server;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tech.dcyao.Iservice.HttpService;
import com.tech.dcyao.service.HttpClientService;
import com.tech.dcyao.utils.Utils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;

@Slf4j
public class EchoHandler implements MessageHandler {
    private ObjectMapper mapper = new ObjectMapper();
    private HttpService httpService = new HttpClientService() ;
    private String baseUrl = "http://127.0.0.1:7001/respberry/";// "http://192.168.0.10:7001/respberry/";
    private String otherUrl = "http://192.168.0.10:7004/respberry/";

    private HashMap<String, String> map = new HashMap<>();
    @Override
    public void onReceive(Connection connection, String message) throws IOException {
        log.info("接受消息：{}", message);
        this.httpService.setBaseUrl(baseUrl);
    //  接受到消息后需要解析对应的消息类似是否为如手动拍照或者是否为定时拍照
        // 校验消息的合理性
        int length = 0;
        String subUrl = "";
        String json = "";
        String result = "";
        if (message.substring(0, 4).equals("AA75")) {
            // 这个时候再获取数据的长度
            String hexStr = message.substring(6, 8);
            log.info("hexStr: {}", hexStr);
            length = Integer.parseInt(hexStr, 16);
            // 判断当前的数据长度是否和当前的长度一致
            if(message.length() == ((length) * 2 + 10)) {
                log.info("长度相同且校验位一致为合法校验数据");
                String codeType = message.substring(4, 6);
                // 增加数据响应的值 前置的值
                String preCode = "557A";
                String code = "";
                String bbcCode = "";
                switch(codeType) {
                    // 相机开
                    case "11":
                        subUrl = "GPIOController";
                        map.put("send", "on");
                        json = mapper.writeValueAsString(map);
                        result = this.httpService.sendPostRequest(subUrl,json);
                        log.info("result" + result);
                        code = preCode + "11" + "01";
                        bbcCode = Utils.setHexArrayToBCC(code);
                        message =  code + bbcCode;
                        break;
                    case "22":
                        subUrl = "GPIOController";
                        map.put("send", "off");
                        json = mapper.writeValueAsString(map);
                        result = this.httpService.sendPostRequest(subUrl,json);
                        log.info("result" + result);
                        code = preCode + "22" + "01";
                        bbcCode = Utils.setHexArrayToBCC(code);
                        message =  code + bbcCode;
                        break;
                    case "33":
                        // 手动拍照命令
                        subUrl = "GPIOController";
                        map.put("send", "photo");
                        json = mapper.writeValueAsString(map);
                        result = this.httpService.sendPostRequest(subUrl,json);
                        log.info("result" + result);
                        code = preCode + "33" + "01";
                        bbcCode = Utils.setHexArrayToBCC(code);
                        message =  code + bbcCode;
                        break;
                    // 取消定时拍照
//
                    case "44":
                        String timeStr = message.substring(8, 10);
                        subUrl = "GPIOControllerIntertime";
                        // 时间间隔和单位
                        int time = Integer.parseInt(timeStr, 16);
                        map.put("send", "photo");
                        map.put("defineTime", time + "");
                        map.put("unit", "s");
                        map.put("timeOut", (time * 1000) + "");
                        json = mapper.writeValueAsString(map);
                        result = this.httpService.sendPostRequest(subUrl,json);
                        log.info("result" + result);
                        code = preCode + "44" + "01";
                        bbcCode = Utils.setHexArrayToBCC(code);
                        message =  code + bbcCode;
                        // 定时拍照命令
                        break;
                    case "66":
                        // 取消定时
                        subUrl = "GPIOControllerIntertime";
                        map.put("send", "noPhoto");
                        // 开始下载
                        json = mapper.writeValueAsString(map);
                        result = this.httpService.sendPostRequest(subUrl,json);
                        log.info("result" + result);
                        code = preCode + "66" + "01";
                        bbcCode = Utils.setHexArrayToBCC(code);
                        message =  code + bbcCode;
                        break;
                    case "1E":
                     // AA751E020100C2 01 为开始下载 00 为结束下载
                        // 结束下载
                        String isDownloadStart = message.substring(8, 10);
                        // 时间间隔和单位
                        subUrl = "GPIOController";
                        // 表示下载开始
                        if(isDownloadStart.equals("01")) {
                            map.put("send", "downloadStart");
                            json = mapper.writeValueAsString(map);
                            result = this.httpService.sendPostRequest(subUrl,json);
                        }
                        // 表示为下载结束
                        if(isDownloadStart.equals("00")) {
                            map.put("send", "downloadEnd");
                            json = mapper.writeValueAsString(map);
                            result = this.httpService.sendPostRequest(subUrl,json);
                        }
                        code = preCode + "1E" + "01";
                        bbcCode = Utils.setHexArrayToBCC(code);
                        message =  code + bbcCode;
                        log.info("result" + result);
                        break;

//                    case "5E":
//                        // 开始复制文件夹
//                        subUrl = "GPIOController";
//                        map = new HashMap<>();
//                        map.put("send", "downloadStart");
//                        json = mapper.writeValueAsString(map);
//                         // 下载开始了
//                        this.httpService.sendPostRequest(subUrl,json);
//                        // 开始提交数据为post请求文件夹
//                        map = null;
//                        subUrl = "usb/checkAndCopy";
//                        map = new HashMap<>();
//                        map.put("usbDrivePath", "/media/pi/CANON_DC/DCIM");
//                        map.put("targetPath", "/media/pi/66F9-6D63/DOWN_LOAD");
//                        json = mapper.writeValueAsString(map);
//                        this.httpService.setBaseUrl(otherUrl);
//                        this.httpService.sendPostRequest(subUrl,json);
//                        // 复制的时间有点慢怎么办
//                        map = null;
//                        subUrl = "GPIOController";
//                        map = new HashMap<>();
//                        map.put("send", "downloadEnd");
//                        json = mapper.writeValueAsString(map);
//                        this.httpService.setBaseUrl(baseUrl);
//                        this.httpService.sendPostRequest(subUrl,json);
//                        // 结束复制文件夹
//                        code = preCode + "5E" + "01";
//                        bbcCode = Utils.setHexArrayToBCC(code);
//                        message =  code + bbcCode;
                }
            }

        }

        connection.println(message );
    }
}